<?php

namespace Frieza\Modules\Position\Actions;

use Doraemon\cache\Queue;
use Doraemon\model\AdmOperateLog;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
use Doraemon\model\AdmRecommendUser;
use Doraemon\model\AdmBestUser;
use Doraemon\model\Member;
use Doraemon\service\Themis\Log as ThemisLog;
use Doraemon\model\Themis\Log as ThemisLogModel;

class CancelFrozen extends RowAction
{
    public $name = '取消冻结';
    const TOAST_ALERT = '您确定要启用此用户吗？';

    public function handle($uid)
    {
        $data['status'] = Member::STATUS_NORMAL;
        $admin_id = Admin::user()->id;
        $admin_name = Admin::user()->username;
        $rst = Member::updateUserInfo($uid,$data);
        // 添加themis日志
        $aOpt = array(
            'admin_id'    => $admin_id,
            'remark'      => "该用户账号已被解冻",
            'status'      => ThemisLogModel::STATUS_FROZEN,
            'action_type' => ThemisLogModel::ACTION_TYPE_DEL_FROZEN,
            'illegal_or_punish' => ThemisLogModel::ACTION_PUNISH,
        );
        $aThemisLog = ThemisLog::fetchThemisLogData($uid, $aOpt);
        ThemisLog::add($aThemisLog);
        if ($rst){
            $user_info = Member::getUserInfo($uid);
            $nick = $user_info['nick'];
            $mytype = $user_info['type'];
            Queue::insertQueue('User/addSearch', array(
                'uid' => $uid,
                'nick' => $nick,
                'type' => $mytype,
            ), Queue::QUEUE_COMMON);
            //删除cahce里面的缓存数据
            Queue::insertQueue('User/delUserinfoCache', array(
                'uid' => $uid,
            ), Queue::QUEUE_COMMON);
            AdmOperateLog::addLog($uid,AdmOperateLog::$operate_type['cancel_ban_user'],$admin_id,$admin_name);
            return $this->response()->success('已启用该用户')->refresh();
        }else{
            return $this->response()->error('启用用户失败,请稍后重试');
        }
    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }

        return $key;
    }

    public function dialog()
    {
        $this->confirm(self::TOAST_ALERT);
    }
}
